﻿using System;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigEaterCh.Formularze;
using BigEaterCh.Properties;

namespace BigEaterCh
{
  public partial class FormBigEaterCh : FormZywienie
  {
    public static int ChildFormNumber;
    public static ZywienieEntities Ze;
    public static FormBigEaterCh parentInst;

    public  string Status
    {
      set { this.toolStripStatusLabel.Text = value; }
    }
    public static void GetDataBase()
    {
      try
      {
        Ze = new ZywienieEntities();
      }
      catch( Exception )
      {
        Ze = null;
        //var sb = new StringBuilder();
        //sb.AppendLine(ex.Message);
        //sb.AppendLine("");
        //if( ex.InnerException!=null )
        //  sb.AppendLine(ex.InnerException.Message);

        //MessageBox.Show(sb.ToString());
      }
    }

    public static void SaveDataBase()
    {
      try
      {
        int added = FormBigEaterCh.Ze.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Count();
        int modified = FormBigEaterCh.Ze.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).Count();
        int deleted = FormBigEaterCh.Ze.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Count();

        String msg = String.Format("Nowe {0} - Zmodyfikowane {1} - Usunięte {2}", added, modified, deleted);
        //MessageBox.Show(msg, @"Zmiany");
        parentInst.Status = msg;
        Ze.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
      }
      catch( Exception ex )
      {
        var sb = new StringBuilder();
        sb.AppendLine(ex.Message);
        sb.AppendLine("");
        if( ex.InnerException!=null )
          sb.AppendLine(ex.InnerException.Message);

        parentInst.Status = sb.ToString();
        MessageBox.Show(sb.ToString());
      }
    }

    public static void DisposeDataBase()
    {
      try
      {
        Ze.Dispose();
        Ze = null;
      }
      catch( Exception ex )
      {
        var sb = new StringBuilder();
        sb.AppendLine(ex.Message);
        sb.AppendLine("");
        if( ex.InnerException!=null )
          sb.AppendLine(ex.InnerException.Message);

        MessageBox.Show(sb.ToString());
      }
    }


    public FormBigEaterCh()
    {
      InitializeComponent();
      parentInst = this;
    }

    private void ExitToolsStripMenuItemClick(object sender, EventArgs e)
    {
      Close();
    }

    private void CutToolStripMenuItem_Click(object sender, EventArgs e)
    {
    }

    private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
    {
    }

    private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
    {
    }

    private void ToolBarToolStripMenuItemClick(object sender, EventArgs e)
    {
      toolStrip.Visible = toolBarToolStripMenuItem.Checked;
    }

    private void StatusBarToolStripMenuItemClick(object sender, EventArgs e)
    {
      statusStrip.Visible = statusBarToolStripMenuItem.Checked;
    }

    private void CascadeToolStripMenuItemClick(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.Cascade);
    }

    private void TileVerticalToolStripMenuItemClick(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.TileVertical);
    }

    private void TileHorizontalToolStripMenuItemClick(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.TileHorizontal);
    }

    private void ArrangeIconsToolStripMenuItemClick(object sender, EventArgs e)
    {
      LayoutMdi(MdiLayout.ArrangeIcons);
    }

    private void CloseAllToolStripMenuItemClick(object sender, EventArgs e)
    {
      foreach (Form childForm in MdiChildren)
      {
        childForm.Close();
      }
    }


    private void FormPrzedszkoleZywienieFormClosing(object sender, FormClosingEventArgs e)
    {
    }

    private void JadłospisToolStripMenuItemClick(object sender, EventArgs e)
    {
      var form = FormJadlospisy.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void RozliczenieToolStripMenuItemClick(object sender, EventArgs e)
    {
      var form = FormRozliczenie.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void FakturyToolStripMenuItemClick(object sender, EventArgs e)
    {
      var form = FormFaktury.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void ProduktyToolStripMenuItem1Click(object sender, EventArgs e)
    {
      var form = FormProdukty.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void FirmyToolStripMenuItem1Click(object sender, EventArgs e)
    {
      var form = FormFirmy.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void wzToolStripMenuItem1_Click(object sender, EventArgs e)
    {
      var form = FormWz.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void potrawyToolStripMenuItem_Click(object sender, EventArgs e)
    {
      var form = FormPotrawy.Pokaz(this);
      form.Show();
      form.BringToFront();
    }

    private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
    {
      var opcje = new FormOpcje();
      opcje.ShowDialog();
    }
  }
}
